const http = require('./http')
const API_KEY = '4AC0DBADE133AFDA11FA8E91CBB82F6E'

module.exports = class API {
  constructor(options = {}) {
    this.options = options
    this.http = http({
      baseURL: options.baseURL || 'https://yunstore.wuliangye.com.cn/api', // 正式
      debug: true,
      headers: {},
      getToken: async (config) => {
        config.headers.token = await this.getAccessToken()
      },
    })
  }

  // 登录
  async login(memberId, token) {
    return this.http
      .post(
        `/member-service/v1/member/queryMemberDetail/?memberId=${memberId}`,
        {},
        {
          headers: {
            token,
          },
        }
      )
      .then((res) => {
        return res.result
      })
  }

  // 获取微信用户信息unionId
  async getUserInfo(unionId) {
    return this.http
      .get(`/file-service/v1/identification/qryMemberInfo`, {
        params: {
          unionId,
        },
        needLogin: true,
      })
      .then((res) => {
        return res.result
      })
  }

  // 获取token
  async getAccessToken() {
    return this.http
      .post(`/file-service/v1/identification/getYunToken`, {
        key: API_KEY,
      })
      .then((res) => {
        return res.token
      })
  }
}
